﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace PresentationLayer
{
    public partial class frmMain : DevComponents.DotNetBar.RibbonForm
    {
        private List<SuperTabItem> listTabs = new List<SuperTabItem>();

        private void superTabControl_TabItemClose(object sender, SuperTabStripTabItemCloseEventArgs e)
        {
            SuperTabItem sst = e.Tab as SuperTabItem;
            listTabs.Remove(sst);
        }

        private void CreateTab(UserControl uc, string NameTab)
        {
            if (!SearchTab(NameTab))
            {
                SuperTabItem st = new SuperTabItem();
                SuperTabControlPanel pn;
                uc.Dock = DockStyle.Fill;
                st = superTabControl.CreateTab(NameTab);
                superTabControl.SelectedTabIndex = superTabControl.Tabs.Count - 1;
                pn = (SuperTabControlPanel)st.AttachedControl;
                pn.Controls.Add(uc);
                listTabs.Add(st);
            }
        }
        private bool SearchTab(string NameText)
        {
            for (int i = 0; i < listTabs.Count; i++)
            {
                if (listTabs[i].Text == NameText)
                {
                    superTabControl.SelectedTabIndex = i;
                    return true;
                }
            }
            return false;
        }

        public frmMain()
        {
            InitializeComponent();
            EnableTask(false);
            btnLogin.Enabled = true;
            btnLogOut.Enabled = btnChangPassword.Enabled = false;
        }

        private void biAboutUs_Click(object sender, EventArgs e)
        {
            frmAbout frm = new frmAbout();
            frm.ShowDialog();
        }

        private void btnService_Click(object sender, EventArgs e)
        {
            ucService ser = new ucService();
            CreateTab(ser, "Dịch vụ");
        }

        private void btnBill_Click(object sender, EventArgs e)
        {
            ucReceipt bill = new ucReceipt();
            CreateTab(bill, "Phiếu nhận");
        }

        private void btnEntryBill_Click(object sender, EventArgs e)
        {
            ucImport bill = new ucImport();
            CreateTab(bill, "Nhập hàng");
        }

        private void btnOption_Click(object sender, EventArgs e)
        {
            frmOption frm = new frmOption();
            frm.ShowDialog();
        }
        void EnableTask(bool t)
        {
            btnBill.Enabled = btnEntryBill.Enabled = btnOption.Enabled = btnService.Enabled = t;
            biOption.Enabled = t;
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            frmLogIn frm = new frmLogIn();
            frm.ShowDialog();
            if (frm.isSuccess)
            {
                EnableTask(true);
                btnLogin.Enabled = false;
                btnLogOut.Enabled = btnChangPassword.Enabled = true;
            }
        }

        void CloseTab()
        {
            for (int i = listTabs.Count - 1; i >= 0; i--)
            {
                superTabControl.CloseTab(listTabs[i]);
            }
            listTabs.Clear();
        }

        private void btnLogOut_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn đăng xuất khỏi chương trình không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                CloseTab();
                btnLogin.Enabled = true;
                EnableTask(false);
                btnLogOut.Enabled = btnChangPassword.Enabled = false;
            }
        }

        private void biClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmMain_Shown(object sender, EventArgs e)
        {
            frmLogIn frm = new frmLogIn();
            frm.ShowDialog();
            if (frm.isSuccess)
            {
                EnableTask(true);
                btnLogin.Enabled = false;
                btnLogOut.Enabled = btnChangPassword.Enabled = true;
            }
        }

        private void btnChangPassword_Click(object sender, EventArgs e)
        {
            frmChangPassword frm = new frmChangPassword();
            frm.ShowDialog();

        }
    }
}